#include "networkcontrol.h"
#include <QProcess>

NetworkControl::NetworkControl(QObject *parent) : QObject(parent) {}

bool NetworkControl::addConnection(const QString &devName, DeviceType type, QString *errorMsg) {
    if (type == DeviceType::Ethernet) {
        QProcess proc;
        QStringList args = {"connection", "add", "type", "ethernet", "ifname", devName, "con-name", devName};
        proc.start("nmcli", args);
        bool finished = proc.waitForFinished(5000);
        QString output = QString::fromUtf8(proc.readAllStandardOutput());
        QString err = QString::fromUtf8(proc.readAllStandardError());
        int exitCode = proc.exitCode();
        if (finished && exitCode == 0) {
            return true;
        } else {
            if (errorMsg) *errorMsg = output + "\n" + err;
            return false;
        }
    } else {
        if (errorMsg) *errorMsg = "当前仅支持以太网自动新建连接";
        return false;
    }
}

bool NetworkControl::renameConnection(const QString &oldName, const QString &newName, QString *errorMsg) {
    QProcess proc;
    QStringList args = {"connection", "modify", oldName, "connection.id", newName};
    proc.start("nmcli", args);
    bool finished = proc.waitForFinished(5000);
    QString output = QString::fromUtf8(proc.readAllStandardOutput());
    QString err = QString::fromUtf8(proc.readAllStandardError());
    int exitCode = proc.exitCode();
    if (finished && exitCode == 0) {
        return true;
    } else {
        if (errorMsg) *errorMsg = output + "\n" + err;
        return false;
    }
}
bool NetworkControl::deleteConnection(const QString &conName, QString *errorMsg) {
    Q_UNUSED(conName); Q_UNUSED(errorMsg); return false;
}

bool NetworkControl::enableDevice(const QString &devName, QString *errorMsg) {
    // 查找是否有profile
    QString connName;
    for (const auto &conn : static_cast<NetworkInfo*>(parent())->getAllConnections()) {
        if (conn.device == devName) { connName = conn.name; break; }
    }
    QProcess proc;
    if (!connName.isEmpty()) {
        QStringList args = {"connection", "up", connName};
        proc.start("nmcli", args);
    } else {
        QStringList args = {"device", "connect", devName};
        proc.start("nmcli", args);
    }
    bool finished = proc.waitForFinished(5000);
    QString output = QString::fromUtf8(proc.readAllStandardOutput());
    QString err = QString::fromUtf8(proc.readAllStandardError());
    int exitCode = proc.exitCode();
    if (finished && exitCode == 0) {
        return true;
    } else {
        if (errorMsg) *errorMsg = output + "\n" + err;
        return false;
    }
}

bool NetworkControl::disableDevice(const QString &devName, QString *errorMsg) {
    // 查找是否有profile
    QString connName;
    for (const auto &conn : static_cast<NetworkInfo*>(parent())->getAllConnections()) {
        if (conn.device == devName) { connName = conn.name; break; }
    }
    QProcess proc;
    if (!connName.isEmpty()) {
        QStringList args = {"connection", "down", connName};
        proc.start("nmcli", args);
    } else {
        QStringList args = {"device", "disconnect", devName};
        proc.start("nmcli", args);
    }
    bool finished = proc.waitForFinished(5000);
    QString output = QString::fromUtf8(proc.readAllStandardOutput());
    QString err = QString::fromUtf8(proc.readAllStandardError());
    int exitCode = proc.exitCode();
    if (finished && exitCode == 0) {
        return true;
    } else {
        if (errorMsg) *errorMsg = output + "\n" + err;
        return false;
    }
} 